﻿using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Smart_Archives.Entity;
using Smart_Archives.Models.Enum;
using Smart_Archives.Utils;

namespace Smart_Archives.Controllers
{
    /// <summary>
    /// 管理员控制器
    /// </summary>
    public class UserController : Controller
    {
        private readonly SaContext _saContext;

        public UserController(SaContext saContext)
        {
            _saContext = saContext;
        }
        public ViewResult Index()
        {
            var pageIndex = Request.Query["page"].ToInt(1);
            var keyword = Request.Query["keyword"];
            //根据更新时间倒序获取管理员列表
            var items = _saContext.User.AsNoTracking().OrderByDescending(a=>a.UpdateTime);

            var pageSize = 10;
            if (!string.IsNullOrEmpty(keyword))
            {
                items = items.Where(a => a.Realname.Contains(keyword) || a.Username.Contains(keyword) || a.Phone.Contains(keyword)).OrderByDescending(a => a.UpdateTime);
                ViewBag.keyword = keyword;
            }

            var itemCount = items.Count();
            var pageCount = (int)Math.Ceiling(itemCount / (double)pageSize);
            var list = items.OrderByDescending(a => a.UpdateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            ViewBag.itemCount = itemCount;
            ViewBag.pageCount = pageCount;
            return View(list);
        }

        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UserDel()
        {
            var userId = Request.Form["id"].ToInt();
            if (userId <= 0)
                return Json("删除失败");
            var model = _saContext.User.Find(userId);
            model.Status = 0;
            _saContext.SaveChanges();
            return Json("删除成功!");

        }

        /// <summary>
        /// 管理员新增/编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult UserEdit()
        {
            var id = Request.Query["id"].ToInt();
            var model=new User();
            if (id>0)
            {
                model = _saContext.User.Find(id);
            }
            return View(model);
        }

        /// <summary>
        /// 管理员更新/编辑处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UserEditProcess()
        {
            try
            {
                var id = Request.Form["id"].ToInt();
                var password = Request.Form["password"];
                var realname = Request.Form["realname"];
                var username = Request.Form["username"];
                var phone = Request.Form["phone"];
                var model = new User();
                if (id > 0)
                {
                    model = _saContext.User.Find(id);

                }
                //2次md5加密
                model.Password = Md5Util.Md5Hash(Md5Util.Md5Hash(password));
                model.Phone = phone;
                model.Realname = realname;
                model.Username = username;
                model.UpdateTime = DateTime.Now;
                //增加时候判断是否用户名重复
                if (id <= 0)
                {
                    var flag = _saContext.User.FirstOrDefault(a => a.Username == username);
                    if (flag != null)
                    {
                        return Json(new { succeed = false });
                    }
                    model.CreateTime = DateTime.Now;
                    model.Status = (int)StatusEnum.Use;
                    _saContext.Add(model);
                }

                _saContext.SaveChanges();
                return Json(new { succeed = true });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return Json(new { succeed = true });
            }
            
        }
    }
}